/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author gulben
 */
public class User {
    private int id;
    private String username;
    private String password;
    
    
    public User() {
        
    }
    
    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    public Customer getUser() {
        Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        User user = (User) session.get("user");
        String username = (String) user.getUsername();
        EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Customer.findByCustomerUsername");
        query.setParameter("username", username);
        List<Customer> customerList = (List<Customer>)query.getResultList();
        return customerList.get(0);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}
